package com.example.musicplayer.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.example.musicplayer.R;
import com.example.musicplayer.commonUtils.ToastUtil;
import com.example.musicplayer.provider.DBConstants;
import com.example.musicplayer.user.User;
import com.example.musicplayer.user.UserManage;

public class SecurityQuestionActivity extends BaseSettingsActivity {
    public static final int RESULT_CODE = 800;
    public static final int MODE_MODIFY = 1;
    public static final int MODE_CONFIRM = 2;
    public static final String ACTION_MODE = "actionMode";
    public static final String MODIFY_SUCCESS = "modifySuccess";
    private int mActionMode;
    private String mUserName = "";
    private EditText mInputQuestion;
    private EditText mInputAnswer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        mActionMode = getIntent().getIntExtra(ACTION_MODE, MODE_CONFIRM);
        if (mActionMode == MODE_CONFIRM) {
            mUserName = getIntent().getStringExtra(DBConstants.User.NAME);
        }
        if (getIntent().getBooleanExtra(LoginActivity.FORGET_PASSWORD, false)) {
            mHasLoginCheck = false;
        }
        super.onCreate(savedInstanceState);
        setEnableMusicTabView(false);
        loadEditView();
        setResult(RESULT_CODE);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        UserManage.updatePasswordConfirmState();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        if (resultCode == PasswordModifyActivity.RESULT_CODE) {
            if (data != null && data.getBooleanExtra(PasswordModifyActivity.MODIFY_SUCCESS, false)) {
                ToastUtil.makeToast(getString(R.string.password_modify_success));
                finishActivity();
            }
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    protected int setActivityTitle() {
        if (mActionMode == MODE_MODIFY) {
            return R.string.title_security_question_activity;
        } else {
            return R.string.title_security_question_activity_confirm;
        }
    }

    private void loadEditView() {
        LayoutInflater inflater = LayoutInflater.from(this);
        @SuppressLint("InflateParams") View editView = inflater.inflate(R.layout.security_question_view, null);
        RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT
                , RelativeLayout.LayoutParams.WRAP_CONTENT);
        lp.addRule(RelativeLayout.CENTER_IN_PARENT);
        ((RelativeLayout) findViewById(R.id.relative_layout_base_settings)).addView(editView, lp);
        //edit text
        mInputQuestion = editView.findViewById(R.id.edittext_input_question);
        mInputAnswer = editView.findViewById(R.id.edittext_input_answer);
        TextView mQuestionText = editView.findViewById(R.id.textview_display_question);
        editView.findViewById(R.id.button_confirm).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                doConfirmAction();
            }
        });
        View clearButton = editView.findViewById(R.id.button_clear);
        clearButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mInputQuestion.setText("");
                mInputAnswer.setText("");
                doConfirmAction();
            }
        });
        //init
        if (mActionMode == MODE_MODIFY) {
            String question = User.getInstance().getPasswordProtect();
            mQuestionText.setVisibility(View.GONE);
            mInputQuestion.setText(question);
            mInputAnswer.setText(question.equals("") ? "" : User.getInstance().getPasswordProtectAnswer());
        } else if (mActionMode == MODE_CONFIRM) {
            mQuestionText.setText(UserManage.getPasswordProtect(mUserName));
            mQuestionText.setVisibility(View.VISIBLE);
            mInputQuestion.setVisibility(View.GONE);
            editView.findViewById(R.id.first_divide).setVisibility(View.GONE);
            clearButton.setVisibility(View.GONE);
            mInputAnswer.setText("");
        }
    }

    private void doConfirmAction() {
        String question = mInputQuestion.getText().toString();
        String answer = mInputAnswer.getText().toString();
        if (mActionMode == MODE_MODIFY) {
            if (question.length() > 0 && answer.equals("")) {
                ToastUtil.makeToast(getString(R.string.protect_answer_empty));
            } else {
                if (User.getInstance().setPasswordProtect(question, answer)) {
                    ToastUtil.makeToast(getString(R.string.set_success));
                    Intent intent = new Intent();
                    intent.putExtra(MODIFY_SUCCESS, true);
                    setResult(RESULT_CODE, intent);
                } else {
                    ToastUtil.makeToast(getString(R.string.set_fail));
                }
            }
        } else if (mActionMode == MODE_CONFIRM) {
            if (UserManage.checkPasswordProtect(mUserName, answer)) {
                Intent intent = new Intent(this, PasswordModifyActivity.class);
                intent.putExtra(DBConstants.User.NAME, mUserName);
                intent.putExtra(LoginActivity.FORGET_PASSWORD, true);
                startActivityForResult(intent, 1);
            } else {
                ToastUtil.makeToast(getString(R.string.security_question_confirm_fail));
                finishActivity();
            }
        }
    }
}